import userConfigStore from '@renderer/store/userConfigStore'
import { VideoState, VideoType } from '@renderer/type'
import { ElMessage, UploadRequestOptions } from 'element-plus'

export default () => {
  const { config } = userConfigStore()
  // 添加文件
  const addFile = (options: UploadRequestOptions) => {
    const name = options.file.name
    const path = options.file.path
    config.files.push({ name, path, progress: 0, state: VideoState.READY })
  }

  // 删除一项
  const removeItem = (index: number) => {
    try {
      const video = config.files[index]
      if (video.state === VideoState.COMPRESS) ElMessage.warning('请等待视频压缩完成！')
      // ElMessageBox.confirm('确定删除吗?')
      else config.files.splice(index, 1)
    } catch (error) {
      console.log('删除失败', error)
    }
  }

  // 全部删除
  const removeAll = () => {
    config.files = []
  }

  // 重置视频
  const resetAllVideo = () => {
    config.files.forEach((item) => {
      item.progress = 0
      item.state = VideoState.READY
    })
  }

  // 获取视频背景颜色
  const bgColor = (video: VideoType) => {
    return {
      [VideoState.READY]: '#ffffff',
      [VideoState.COMPRESS]: '#F9F871',
      [VideoState.ERROR]: '#FF6F91',
      [VideoState.FINISH]: '#4FFBDF'
    }[video.state]
  }

  return { addFile, removeItem, removeAll, resetAllVideo, bgColor }
}
